package com.antler.smt.portal.controller;

import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.annotation.OperateCut;
import com.antler.smt.request.portal.GetReceivingAddressReq;
import com.antler.smt.request.portal.OrderCommentReq;
import com.antler.smt.request.portal.PurchaserPersonReq;
import com.antler.smt.response.admin.*;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.portal.GetPurchaserAccountResp;
import com.antler.smt.service.portal.*;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *  采购商个人中心
 *
 * @author zhangyi
 * @date 2018/3/27 10:15
 */
@RestController
@RequestMapping("/purchaserCenter")
@Api(description = "采购商个人中心")
public class PurchaserCenterController extends BaseController {

    /**
     * 日志
     */
    public static Logger log = LogUtil.get();

    @Resource
    private PurchaserService purchaserService;

    @Resource
    private ReceivingAddressService receivingAddressService;

    @Resource
    private OrderService orderService;

    @Resource
    private MemberAccountService memberAccountService;

    @Resource
    private OrderCommentService orderCommentService;

    @Resource
    private PurchaserChargeService purchaserChargeService;

    /**
     * 获取当前的采购商实体（个人资料）
     *
     * @return 采购商实体
     */
    @GetMapping("/getPurchaser")
    @ApiOperation(value = "获取当前的采购商实体（个人资料）", httpMethod = "GET")
    @OperateCut(operationContent = "查看个人资料")
    public Result<GetPurchaserListResp> getPurchaser() {
        Result result = null;
            result = purchaserService.getPurchaserInfo(currentMember.getId());
        return result;
    }

    /**
     * 获取采购商的收获地址列表
     *
     * @return
     */
    @GetMapping("/purchaserAddressList")
    @ApiOperation(value = "获取采购商的收获地址列表", httpMethod = "GET")
    @OperateCut(operationContent = "查看地址")
    public Result<GetReceivingAddressListResp> purchaserAddressList(){
        Result result = null;
            result = receivingAddressService.getpurchaserAddressList(currentMember.getId());
        return result;
    }

    /**
     *  填写收货地址
     *
     * @param request
     * @return
     */
    @PostMapping("/writeReceivingAddress")
    @ApiOperation(value = "填写收货地址", httpMethod = "POST")
    @OperateCut(operationContent = "填写收货地址")
    public Result<ResultCodeEnum> writeReceivingAddress(@RequestBody GetReceivingAddressReq request){

        DataValidator.isNull(request,"请求收货地址地址不能为空！");
        DataValidator.isBlank(request.getCityName(),"城市不能为空！");
        DataValidator.isBlank(request.getCountryName(),"国家不能为空！");
        DataValidator.isBlank(request.getProvinceName(),"省不能为空！");
        DataValidator.isBlank(request.getDetailaddress(),"详细地址不能为空！");
        DataValidator.isBlank(request.getName(),"收件人不能为空！");


        Result result = null;
            result = receivingAddressService.writeReceivingAddress(request,currentMember.getId());
        return result;
    }

    /**
     *  查询一个指定的收货地址
     *
     * @param id 收货地址id
     * @return
     */
    @PostMapping("/getReceivingAddress/{id}")
    @ApiOperation(value = "查询一个指定的收货地址", httpMethod = "POST")
    @OperateCut(operationContent = "查询收货地址")
    public Result<ResultCodeEnum> getReceivingAddress(@PathVariable Long id){

        DataValidator.isBlank(String.valueOf(id),"请求的收货地址id不能为空！");

        Result result = null;
            result = receivingAddressService.getReceivingAddress(id);
        return result;
    }


    /**
     *  更新收货地址
     *
     * @param id 收货地址id
     * @return
     */
    @PostMapping("/updateReceivingAddress/{id}")
    @ApiOperation(value = "更新收货地址", httpMethod = "POST")
    @OperateCut(operationContent = "更新收货地址")
    public Result<ResultCodeEnum> updateReceivingAddress(@RequestBody GetReceivingAddressReq request,
                                                         @PathVariable Long id){
        DataValidator.isNull(request,"请求收货地址不能为空！");
        DataValidator.isBlank(String.valueOf(id),"收货地址id不能为空！");
        Result result = null;
            result = receivingAddressService.updateReceivingAddress(request,id);
        return result;
    }

    /**
     *  删除收货地址
     *
     * @param id 收货地址id
     * @return
     */
    @GetMapping("/delReceivingAddress/{id}")
    @ApiOperation(value = "删除收货地址", httpMethod = "GET")
    @OperateCut(operationContent = "删除收货地址")
    public Result<ResultCodeEnum> delReceivingAddress(@PathVariable Long id){

        DataValidator.isBlank(String.valueOf(id),"请求id不能为空！");

        Result result = null;
            result = receivingAddressService.delReceivingAddress(id);
        return result;
    }
    /**
     *  设置默认收货地址
     *
     * @param
     * @return
     */
    @GetMapping("/defaultReceivingAddress/{id}")
    @ApiOperation(value = "设置默认收货地址", httpMethod = "GET")
    @OperateCut(operationContent = "设置默认收货地址")
    public Result<ResultCodeEnum> defaultReceivingAddress(@PathVariable Long id){

        DataValidator.isBlank(String.valueOf(id),"请求地址id不能为空！");

        Result result = null;
            result = receivingAddressService.defaultReceivingAddress(id);
        return result;
    }

    /**
     *  编辑个人信息
     *
     * @param purchaserPersonReq
     * @return
     */
    @PostMapping("/editMemberPurchaserInfo")
    @ApiOperation(value = "编辑个人信息", httpMethod = "POST")
    @OperateCut(operationContent = "编辑个人信息")
    public Result<ResultCodeEnum> editMemberPurchaserInfo(@RequestBody PurchaserPersonReq purchaserPersonReq){

        DataValidator.isNull(purchaserPersonReq,"个人信息请求实体不能为空！");
        Result result = null;

        result = purchaserService.editPurchaserInfo(purchaserPersonReq,currentMember.getId());
        return result;
    }

    /**
     * 分类获取采购商的订单 2已完成 1未完成 未通过
     * 已完成：6待评价 7待付款给服务商 8结束
     * 未完成：0待对接服务商 1待付款 2待确认付款 3待生产完成  4待收货
     * 未通过：100驳回
     *
     * @param orderStatus 订单状态
     * @return
     */
    @PostMapping("/purchaserClassifyOrderList/{orderStatus}")
    @ApiOperation(value = "* 已完成：6待评价 7待付款给服务商 8结束 未完成：0待对接服务商 1待付款 2待确认付款 3待生产完成  4待收货 未通过：100驳回", httpMethod = "POST")
    @OperateCut(operationContent = "分类查看订单信息")
    public Result<GetOrderListResp> purchaserClassifyOrderList(@PathVariable Integer orderStatus){

        DataValidator.isBlank(String.valueOf(orderStatus),"订单状态！不能为空");

        Result result = null;
            result = orderService.getPurchaserOrderClassifyInfo(currentMember.getId(),orderStatus);
        return result;
    }
    /**
     * 获取采购商的全部订单
     *
     * @return
     */
    @GetMapping("/purchaserOrderList")
    @ApiOperation(value = "获取采购商的全部订单", httpMethod = "GET")
    @OperateCut(operationContent = "查看全部订单")
    public Result<GetOrderListResp> purchaserOrderList(){
        Result result = null;
            result  = purchaserService.purchaserOrderList(currentMember.getId());
        return result;
    }

    /**
     *  获取当前的订单详细情况
     *
     * @param id 请求的订单id值
     * @return
     */
    @GetMapping("/purchaserOrderList/{id}")
    @ApiOperation(value = "获取当前的订单详细情况", httpMethod = "GET")
    public Result<GetOrderListResp> getOrderDetail(@PathVariable Long id){

        DataValidator.isBlank(String.valueOf(id),"订单id不为空！");

        Result result = null;
           result = orderService.getOrderList(id,currentMember.getId());
        return result;
    }

    /**
     * 获取会员账户信息实体我的VIP
     *
     * @return
     */
    @GetMapping("/memberAccountInfo")
    @ApiOperation(value = "获取会员账户信息实体我的VIP", httpMethod = "GET")
    @OperateCut(operationContent = "查看我的账户和VIP信息")
    public Result<GetPurchaserAccountResp> getMemberAccountInfo(){
        Result result = null;
            result = memberAccountService.getMemberAccount(currentMember.getId());
        return result;
    }

    /**
     *  采购商确认收货  收到货之后，整体订单流程结束（可以不评价）
     *
     * @param orderId 订单id
     * @return
     */
    @PostMapping("/getGoods/{orderId}")
    @ApiOperation(value = "采购商确认收货，收到货之后，整体订单流程结束（可以不评价）", httpMethod = "POST")
    @OperateCut(operationContent = "采购商确认收货")
    public Result<ResultCodeEnum> getGoods(@PathVariable Long orderId){

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");

        Result result = null;
            result = orderService.getGoods(orderId);
        return result;
    }

    /**
     *  采购商充值
     *
     * @param ammount 充值金额
     * @return
     */
    @PostMapping("/charge")
    @ApiOperation(value = "采购商充值", httpMethod = "POST")
    @OperateCut(operationContent = "充值余额")
    public Result<ResultCodeEnum> charge(@RequestParam(value = "ammount") Long ammount){

        DataValidator.isBlank(String.valueOf(ammount),"充值金额必传！");

        Result result = null;
            result = purchaserChargeService.purchaserChaser(currentMember.getId(),ammount);
        return result;
    }


    /**
     *  采购商评价订单
     *
     * @param orderId 订单id
     * @param request 请求订单评价实体
     * @return
     */
    @PostMapping("/orderComment/{orderId}")
    @ApiOperation(value = "采购商评价订单", httpMethod = "POST")
    @OperateCut(operationContent = "评价订单")
    public Result<ResultCodeEnum> orderComment(@PathVariable Long orderId,
                                               @RequestBody OrderCommentReq request){

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");
        DataValidator.isNull(request,"订单评价不为空！");

        DataValidator.isBlank(request.getToPlatform(),"对平台评价不为空！");
        DataValidator.isBlank(request.getToSupplier(),"对服务商评价不为空！");
        DataValidator.isBlank(String.valueOf(request.getGrade()),"星级不能为空！");

        DataValidator.isBlank(String.valueOf(request.getAttachmentIds()),"评价图片数组不能为空！");

        Result result = null;
            result = orderCommentService.insertOrderComment(orderId,request,request.getAttachmentIds());
        return result;
    }

    /**
     *  采购商生成账户
     *
     * @return
     */
    @PostMapping("/createAccount")
    @ApiOperation(value = "采购商生成账户实体", httpMethod = "POST")
    @OperateCut(operationContent = "采购商账户生成")
    public Result<ResultCodeEnum> createAccount(){
        Result result = null;
            result = memberAccountService.purchaserCreateAccount(currentMember.getId());
        return result;
    }


    /**
     *  采购商距离下次升级的金额
     *
     * @return
     */
    @GetMapping("/nextGoUpMoney")
    @ApiOperation(value = "采购商距离下次升级的金额", httpMethod = "GET")
    @OperateCut(operationContent = "查看距离下次升级的金额")
    public Result nextGoUpMoney(){
        Result result = null;
        result = purchaserService.nextGoUpMoney(currentMember.getId());
        return result;
    }

}
